package com.ghck.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ghck.server.model.NurseStationDict;
import com.ghck.server.model.query.NurseStationDictQuery;
import com.ghck.server.utils.IntegerUtil;
/**
 * 护士站  <br>
 * dict_nurse_station <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.NurseStationDictMapper
 */
 public class NurseStationDictProvider {
 
 	/**
 	* 新增 护士站
 	*/
 	public String insert(NurseStationDict domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into dict_nurse_station(create_time,last_modify_time");
	 	if(domain.getName()!=null){
			sb.append(",name");
		}
	 	if(domain.getServiceTime()!=null){
			sb.append(",service_time");
		}
	 	if(domain.getTel()!=null){
			sb.append(",tel");
		}
	 	if(domain.getRegionId()!=null){
			sb.append(",region_id");
		}
	 	if(domain.getAddress()!=null){
			sb.append(",address");
		}
	 	if(domain.getLongitude()!=null){
			sb.append(",longitude");
		}
	 	if(domain.getLatitude()!=null){
			sb.append(",latitude");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getName()!=null){
			sb.append(",#{name}");
		}
	 	if(domain.getServiceTime()!=null){
			sb.append(",#{serviceTime}");
		}
	 	if(domain.getTel()!=null){
			sb.append(",#{tel}");
		}
	 	if(domain.getRegionId()!=null){
			sb.append(",#{regionId}");
		}
	 	if(domain.getAddress()!=null){
			sb.append(",#{address}");
		}
	 	if(domain.getLongitude()!=null){
			sb.append(",#{longitude}");
		}
	 	if(domain.getLatitude()!=null){
			sb.append(",#{latitude}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 护士站
 	*/
	public String update(NurseStationDict domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update dict_nurse_station set last_modify_time = now() ");
		
		if(domain.getName()!=null){
			sb.append(",name=#{name}");
		}
		if(domain.getServiceTime()!=null){
			sb.append(",service_time=#{serviceTime}");
		}
		if(domain.getTel()!=null){
			sb.append(",tel=#{tel}");
		}
		if(domain.getRegionId()!=null){
			sb.append(",region_id=#{regionId}");
		}
		if(domain.getAddress()!=null){
			sb.append(",address=#{address}");
		}
		if(domain.getLongitude()!=null){
			sb.append(",longitude=#{longitude}");
		}
		if(domain.getLatitude()!=null){
			sb.append(",latitude=#{latitude}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<NurseStationDict>> map){
		StringBuffer sb = new StringBuffer();
		List<NurseStationDict> nurseStationDicts = map.get("list");
		sb.append("insert into dict_nurse_station(create_time,last_modify_time,name,service_time,tel,region_id,address,longitude,latitude,in_used) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].name}"
	    		+ ",#'{'list[{0}].serviceTime}"
	    		+ ",#'{'list[{0}].tel}"
	    		+ ",#'{'list[{0}].regionId}"
	    		+ ",#'{'list[{0}].address}"
	    		+ ",#'{'list[{0}].longitude}"
	    		+ ",#'{'list[{0}].latitude}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(nurseStationDicts)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  nurseStationDicts.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 护士站
 	*/
	public String findByQuery(NurseStationDictQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from dict_nurse_station where 1>0 ");
		List<Integer> regionIds = query.getRegionIds();
		if (CollectionUtils.isNotEmpty(regionIds)) {
			sb.append(" and region_id in (#{regionIds[0]}");
			for (int i = 1; i < regionIds.size(); i++) {
				sb.append(",#{regionIds[").append(i).append("]}");
			}
			sb.append(")");
		}
		if(StringUtils.isNotEmpty(query.getKeyword())){
			sb.append(" and name like #{keyword}");
		}
		if(IntegerUtil.isPositiveInteger(query.getStartId())){
			sb.append(" and id>#{startId} order by id limit #{limit}");
		}else if(IntegerUtil.isPositiveInteger(query.getEndId())){
			sb.append(" and id<#{endId} order by id desc limit #{limit}");
		}else if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(NurseStationDictQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from dict_nurse_station a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 护士站
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from dict_nurse_station where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	